/*     */ package com.newcrm.webapi.sub;
/*     */ 
/*     */ import com.newcrm.bean.CustomerSearchCondition;
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.db.CustomerManager;
/*     */ import com.newcrm.db.UserManager;
/*     */ import com.newcrm.exception.NotFoundException;
/*     */ import com.newcrm.thread.LuceneThread;
/*     */ import com.newcrm.util.Util;
/*     */ import com.newcrm.webapi.AbstractAction;
/*     */ import com.newcrm.webapi.AbstractHttpServlet;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Date;
/*     */ import java.util.List;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import net.sf.json.JSONObject;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class ChangeCustomerVisibility extends AbstractAction
/*     */ {
/*  37 */   private static Logger logger = Logger.getLogger(ChangeCustomerVisibility.class.getName());
/*     */ 
/*  39 */   private String indexPath = null;
/*     */ 
/*     */   public ChangeCustomerVisibility(AbstractHttpServlet servlet, String indexPath)
/*     */   {
/*  43 */     super(servlet);
/*  44 */     this.indexPath = indexPath;
/*     */   }
/*     */ 
/*     */   public void parse(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException, ServletException, NotFoundException
/*     */   {
/*  50 */     User loginUser = getLoginUser(request);
/*  51 */     String userId = loginUser.userId;
/*     */ 
/*  53 */     response.setContentType("text/html;charset=utf-8");
/*  54 */     PrintWriter out = response.getWriter();
/*  55 */     JSONObject jsonObject = new JSONObject();
/*  56 */     String changePermissionsId = Util.getURLParameter(request
/*  57 */       .getParameter("ChangePermissionsId"));
/*     */ 
/*  60 */     if (changePermissionsId == null) {
/*  61 */       jsonObject.put("error", "1");
/*  62 */       out.println(jsonObject);
/*  63 */       out.flush();
/*  64 */       out.close();
/*  65 */       return;
/*     */     }
/*     */ 
/*  69 */     String[] changePermissionsIds = changePermissionsId.split(",");
/*  70 */     int changeVisible = Util.getURLIntParameter(request
/*  71 */       .getParameter("ChangeVisible"));
/*     */ 
/*  73 */     String types = Util.getURLParameter(request.getParameter("Types"));
/*  74 */     String customerIds = Util.getURLParameter(request
/*  75 */       .getParameter("CustomerIds"));
/*     */ 
/*  77 */     String selectAll = Util.getURLParameter(request
/*  78 */       .getParameter("selectAll"));
/*     */     int limitNum;
/*     */     int result;
/*     */     int loopIndex;
/*  82 */     if ("1".equals(selectAll)) {
/*  83 */       int totalNums = Util.getURLIntParameter(request
/*  84 */         .getParameter("totalNums"));
/*     */ 
/*  87 */       if (changeVisible == 4) {
/*  88 */         for (String eachChangePermissionsId : changePermissionsIds)
/*     */         {
/*  92 */           if (isOverMaxCompanyNum(eachChangePermissionsId, 
/*  90 */             changeVisible, totalNums / 
/*  91 */             changePermissionsIds.length + totalNums % 
/*  92 */             changePermissionsIds.length)) {
/*  93 */             jsonObject.put("error", "3");
/*  94 */             jsonObject.put("changePermissionsId", 
/*  95 */               eachChangePermissionsId);
/*  96 */             out.println(jsonObject);
/*  97 */             out.flush();
/*  98 */             out.close();
/*  99 */             return;
/*     */           }
/*     */         }
/*     */       }
/*     */ 
/* 104 */       CustomerSearchCondition searchCondition = new CustomerSearchCondition(
/* 105 */         request, loginUser, false);
/* 106 */       CustomerManager manager = new CustomerManager();
/* 107 */       limitNum = 0;
/*     */ 
/* 109 */       if (changePermissionsIds.length == 1) {
/* 110 */         limitNum = 0;
/*     */       }
/* 112 */       else if (totalNums <= changePermissionsIds.length)
/* 113 */         limitNum = 1;
/*     */       else {
/* 115 */         limitNum = totalNums / changePermissionsIds.length;
/*     */       }
/*     */ 
/* 119 */       result = 0;
/* 120 */       loopIndex = 1;
/* 121 */       boolean deleteFlag = true;
/*     */ 
/* 123 */       String nowTemp = Util.date2Str(new Date());
/* 124 */       Date nowUpdate = Util.str2Date2(nowTemp);
/*     */ 
/* 129 */       for (String eachChangePermissionsId : changePermissionsIds)
/*     */       {
/* 132 */         if (result >= totalNums)
/*     */         {
/*     */           break;
/*     */         }
/* 136 */         if (loopIndex == changePermissionsIds.length)
/* 137 */           manager.changeAllVisibility(changeVisible, 
/* 138 */             eachChangePermissionsId, 0, deleteFlag, nowUpdate, 
/* 139 */             searchCondition, loginUser);
/*     */         else {
/* 141 */           manager.changeAllVisibility(changeVisible, 
/* 142 */             eachChangePermissionsId, limitNum, deleteFlag, 
/* 143 */             nowUpdate, searchCondition, loginUser);
/*     */         }
/* 145 */         result += limitNum;
/*     */ 
/* 147 */         deleteFlag = false;
/*     */ 
/* 149 */         loopIndex++;
/*     */       }
/*     */ 
/* 153 */       new LuceneThread(this.indexPath, false).start();
/* 154 */       jsonObject.put("error", "0");
/* 155 */       jsonObject.put("updateNum", Integer.valueOf(totalNums));
/* 156 */       jsonObject.put("updateIds", changePermissionsIds);
/* 157 */       out.println(jsonObject);
/* 158 */       out.flush();
/* 159 */       out.close();
/* 160 */       return;
/*     */     }
/*     */ 
/* 164 */     if ((customerIds == null) || (customerIds.trim().length() == 0) || 
/* 165 */       (types == null) || (types.trim().length() == 0)) {
/* 166 */       jsonObject.put("error", "1");
/* 167 */       out.println(jsonObject);
/* 168 */       out.flush();
/* 169 */       out.close();
/* 170 */       return;
/*     */     }
/*     */ 
/* 173 */     String[] customerType = types.split(",");
/* 174 */     String[] customerId = customerIds.split(",");
/* 175 */     if ((customerType == null) || (customerId == null) || 
/* 176 */       (customerType.length != customerId.length)) {
/* 177 */       jsonObject.put("error", "1");
/* 178 */       out.println(jsonObject);
/* 179 */       out.flush();
/* 180 */       out.close();
/* 181 */       return;
/*     */     }
/*     */ 
/* 185 */     if (changeVisible == 4) {
/* 186 */       for (String eachChangePermissionsId : changePermissionsIds)
/*     */       {
/* 191 */         if (isOverMaxCompanyNum(
/* 188 */           eachChangePermissionsId, 
/* 189 */           changeVisible, 
/* 190 */           customerId.length / changePermissionsIds.length + customerId.length % 
/* 191 */           changePermissionsIds.length)) {
/* 192 */           jsonObject.put("error", "3");
/* 193 */           jsonObject.put("changePermissionsId", 
/* 194 */             eachChangePermissionsId);
/* 195 */           out.println(jsonObject);
/* 196 */           out.flush();
/* 197 */           out.close();
/* 198 */           return;
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/* 203 */     int idx = 0;
/* 204 */     Object updateCustomerIds = new ArrayList();
/* 205 */     CustomerManager manager = new CustomerManager();
/* 206 */     for (int i = 0; i < customerId.length; i++) {
/* 207 */       boolean isPerson = "1".equals(customerType[i]);
/* 208 */       result = 0;
/*     */       try {
/* 210 */         if (idx >= changePermissionsIds.length) {
/* 211 */           idx = 0;
/*     */         }
/* 213 */         String id = changePermissionsIds[idx];
/* 214 */         idx++;
/* 215 */         result = manager.changeVisibility(Util.str2Int(customerId[i]), 
/* 216 */           changeVisible, id, isPerson, userId);
/* 217 */         if (result >= 1)
/* 218 */           ((List)updateCustomerIds).add(customerId[i]);
/*     */       }
/*     */       catch (Exception e) {
/* 221 */         logger.warn("change visible failed", e);
/* 222 */         jsonObject.put("error", "2");
/* 223 */         jsonObject.put("msg", "修改客户信息失败，请稍候再试");
/* 224 */         out.println(jsonObject);
/* 225 */         out.flush();
/* 226 */         out.close();
/* 227 */         return;
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 233 */     new LuceneThread(this.indexPath, false).start();
/*     */ 
/* 235 */     jsonObject.put("error", "0");
/* 236 */     jsonObject.put("updateIds", ((List)updateCustomerIds).toArray());
/* 237 */     out.println(jsonObject);
/* 238 */     out.flush();
/* 239 */     out.close();
/*     */   }
/*     */ 
/*     */   private boolean isOverMaxCompanyNum(String userId, int visible, int addNum)
/*     */   {
/* 252 */     if ((userId == null) || (userId.length() == 0) || 
/* 253 */       (visible == 1)) {
/* 254 */       return false;
/*     */     }
/* 256 */     UserManager userManager = new UserManager();
/* 257 */     String extValue = null;
/*     */     try {
/* 259 */       extValue = userManager.getExtInfoByType(userId, 
/* 260 */         2);
/*     */     } catch (Exception e) {
/* 262 */       logger.warn("get over max company num failed", e);
/*     */     }
/* 264 */     int maxCompanyNum = Util.str2Int(extValue);
/* 265 */     if (maxCompanyNum > 0) {
/* 266 */       CustomerManager customerManager = new CustomerManager();
/* 267 */       int currentCompanyNum = customerManager.getUserCompanyNum(userId);
/* 268 */       if (maxCompanyNum < currentCompanyNum + addNum) {
/* 269 */         return true;
/*     */       }
/*     */     }
/* 272 */     return false;
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.sub.ChangeCustomerVisibility
 * JD-Core Version:    0.6.1
 */